package howl.model;

import howl.adaptor.*;
import howl.types.HowlUser;

import java.util.ArrayList;

import compiler.HowlToken;
import compiler.nonterminals.*;


public class ServiceHandler {
	
	private ArrayList<SimplexAdaptor> services;
	
	private HowlUser user;

	
	public ServiceHandler(HowlUser u) {
		super();
		this.user = u;
		this.setup();
	}
	
	private void setup() {
		this.services = new ArrayList<SimplexAdaptor>();
		if(this.user.isPachube() && this.user.isPachubeisConfigured()) {
			this.services.add(new PachubeAdaptor(user));
		}
		
		if(this.user.isTwitter() && this.user.isTwitterisConfigured()) {
			this.services.add(new TwitterAdaptor(user));
		}
		
		if(this.user.isYourFlowingData() && this.user.isYourFlowingDataisConfigured()) {
			this.services.add(new Yfd(user));
		}
	}
	
	public void handle(HowlToken ht) {
		if (ht instanceof CreateStmt) {
			this.doCreate((CreateStmt) ht);

		} else if (ht instanceof DeleteStmt) {
			this.doDelete((DeleteStmt) ht);

		} else if (ht instanceof DeregisterStmt) {
			this.doDeregister((DeregisterStmt) ht);

		} else if (ht instanceof InsertStmt) {
			this.doInsert((InsertStmt) ht);

		} else if (ht instanceof RegisterStmt) {
			this.doRegister((RegisterStmt) ht);

		}
	}
	
	
	private void doCreate(CreateStmt ht) {
		for(int i = 0; i<this.services.size(); i++) {
			this.services.get(i).doCreate(ht);
		}
	}
	
	private void doDelete(DeleteStmt ht) {
		for(int i = 0; i<this.services.size(); i++) {
			this.services.get(i).doDelete(ht);
		}
	}
	
	private void doInsert(InsertStmt ht) {
		for(int i = 0; i<this.services.size(); i++) {
			this.services.get(i).doInsert(ht);
		}
	}
	
	private void doRegister(RegisterStmt ht) {
		//MAYBE USED AT A LATER POINT
	}
	
	private void doDeregister(DeregisterStmt ht) {
		//MAYBE USED AT A LATER POINT
	}
	
}
